package com.caiden_micheal.dynamic.record.service.impl;

import com.caiden_micheal.base.utils.MinioUtil;
import com.caiden_micheal.dynamic.file_management.service.impl.FileManagementServiceImpl;
import com.caiden_micheal.dynamic.record.domain.DFileRecord;
import com.caiden_micheal.dynamic.record.mapper.DFileRecordMapper;
import com.caiden_micheal.dynamic.record.service.DFileRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class DFileRecordServiceImpl implements DFileRecordService {
    private static final Logger logger = LoggerFactory.getLogger(DFileRecordServiceImpl.class);

    @Autowired
    private DFileRecordMapper dFileRecordMapper;
    @Autowired
    private MinioUtil minioUtil;

    @Override
    public void deleteDeActiveFile(String format) {
        try {
            List<DFileRecord> deActiveFileList = dFileRecordMapper.findDeActiveFile(format);
            if (deActiveFileList != null && deActiveFileList.size() != 0) {
                dFileRecordMapper.deleteDeActiveFile(deActiveFileList);
                for (DFileRecord dFileRecord : deActiveFileList) {
                    minioUtil.removeFile(dFileRecord.getFileName());
                }
            }
        } catch (Exception e) {
            logger.error(e.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
